package ch.zhaw.hgc.controllers;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import ch.zhaw.hgc.entities.Recipe;
import ch.zhaw.hgc.entities.UserAccount;
import ch.zhaw.hgc.services.IUserAccountService;

@Controller
@RequestMapping("/validation*")
public class ValidationController {

	private static final Logger logger = Logger
			.getLogger(ValidationController.class);

	private List<UserAccount> userAccounts;

	@Autowired
	private IUserAccountService userAccountService;

	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView getCampaignDetails(ModelAndView model,
			@RequestParam("val") String validationValue) {

		logger.info("Here: " + validationValue);

		userAccounts = userAccountService.findAll();

		boolean validationOK = false;
		for (int i = 0; i < userAccounts.size(); i++) {

			if (userAccounts.get(i).getValidationValue() != null)
				if (userAccounts.get(i).getValidationValue()
						.equals(validationValue)) {
					// TODO Redirect to Login Site
					userAccounts.get(i).setValidationValue("");
					userAccountService.save(userAccounts.get(i));
					validationOK = true;
					break;
				}

		}

		if (validationOK) {
			
			model.addObject("message", "Successfully validated");
		} else {
			
			model.addObject("message", "Error during validation!!!!");

		}


		logger.info("Passed get() method in controller...");

		return model;

	}

}
